// login 方法参数
let options = {
    token: localStorage.getItem('rtmToken'),
    uid: JSON.parse(localStorage.getItem('user')).user_id
}

async function loginRTM() {

    // 你的 app ID
    const appID = "69ee1d0f77224d1897d21a1d3bc56cd4"

    // 初始化客户端
    const client = AgoraRTM.createInstance(appID)

    //创建频道 文本传输
    let channel1 = client.createChannel(`cuyear${new Date().getFullYear()}_cumonth=${new Date().getMonth() + 1}cuhour=${new Date().getHours()}_courseid${window.location.href.split('/')[6].split('?')[1].split('&')[1].split('=')[1]}_channeltext`)
    //创建频道 文件传输
    let channel2 = client.createChannel(`cuyear${new Date().getFullYear()}_cumonth=${new Date().getMonth() + 1}cuhour=${new Date().getHours()}_courseid${window.location.href.split('/')[6].split('?')[1].split('&')[1].split('=')[1]}_channelfile`)


    // 显示连接状态变化
    client.on('ConnectionStateChanged', function (state, reason) {
        console.log("State changed To: " + state + " Reason: " + reason)
    })

    // 频道成员进入
    channel1.on('MemberJoined', function (memberId) {
        // console.log(memberId + " joined the channel")
    })

    // 频道成员离开
    channel1.on('MemberLeft', function (memberId) {
    //    console.log(memberId + " left the channel")
    })

    // 登录 RTM 系统
    await client.login(options)


    return {
        client,
        channel1,
        channel2
    }
}

export default loginRTM